<?php

/**
 * reglas actions.
 *
 * @package    SistemaRutasEc
 * @subpackage reglas
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class reglasActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->reglas = Doctrine_Core::getTable('regla')
                ->createQuery('a')
                ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->regla = Doctrine_Core::getTable('regla')->find(array($request->getParameter('id_re')));
        $this->forward404Unless($this->regla);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new reglaForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new reglaForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->idco = $request->getParameter('id_co');
        $q = Doctrine_Query::create()
                ->from('regla r')
                ->where('r.id_co=?', $this->idco);
        $regla = $q->fetchOne();


//    $this->forward404Unless($regla = Doctrine_Core::getTable('regla')->find(array($request->getParameter('id_co'))), sprintf('Object regla does not exist (%s).', $request->getParameter('id_co')));
        $this->form = new reglaForm($regla);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($regla = Doctrine_Core::getTable('regla')->find(array($request->getParameter('id_re'))), sprintf('Object regla does not exist (%s).', $request->getParameter('id_re')));
        $this->form = new reglaForm($regla);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($regla = Doctrine_Core::getTable('regla')->find(array($request->getParameter('id_re'))), sprintf('Object regla does not exist (%s).', $request->getParameter('id_re')));
        $regla->delete();

        $this->redirect('reglas/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $regla = $form->save();
            $rutas = Doctrine::getTable('ruta')->createQuery('r')
                    ->where('r.id_co = ?', $regla->getIdCo())
                    ->execute();
            if ($regla->getEspecial() == true) {
                foreach ($rutas as $ru):
                    $escalasnormales = Doctrine::getTable('escalanormal')->createQuery('en')
                            ->where('en.id_ru = ?', $ru->getIdRu())
                            ->execute();
                    foreach ($escalasnormales as $esno):
                        $newescalaespecial = new Escalaespecial();
                        $newescalaespecial->setIdRu($esno->getIdRu());
                        $newescalaespecial->setCiudad($esno->getCiudad());
                        $newescalaespecial->setDistancia($esno->getDistancia());
                        $newescalaespecial->setCostonormal($esno->getCostonormal());
                        $newescalaespecial->setCostoespecial(0);
                        $newescalaespecial->save();
                        $esno->delete();
                    endforeach;
                endforeach;
            } else if ($regla->getEspecial() == false) {
                foreach ($rutas as $ru):
                    $escalasespeciales = Doctrine::getTable('escalaespecial')->createQuery('es')
                            ->where('es.id_ru = ?', $ru->getIdRu())
                            ->execute();
                    foreach ($escalasespeciales as $eses):
                        $newescalanormal = new Escalanormal();
                        $newescalanormal->setIdRu($eses->getIdRu());
                        $newescalanormal->setCiudad($eses->getCiudad());
                        $newescalanormal->setDistancia($eses->getDistancia());
                        $newescalanormal->setCostonormal($eses->getCostonormal());                        
                        $newescalanormal->save();
                        $eses->delete();
                    endforeach;
                endforeach;
            }
            $this->getUser()->setFlash('notice', 'Regla activada/desactivada');
            $this->redirect('cooperativas/index');
        }
    }

}
